package com.qxl.forest.business.controller;

import com.qxl.forest.business.service.RoleService;
import com.qxl.forest.core.annotion.BussinessLog;
import com.qxl.forest.core.annotion.Permission;
import com.qxl.forest.common.constants.Const;
import com.qxl.forest.common.constants.RequestParams;
import com.qxl.forest.common.constants.cache.Cache;
import com.qxl.forest.common.constants.factory.ConstantFactory;
import com.qxl.forest.business.persistence.dao.RoleDao;
import com.qxl.forest.business.persistence.entity.Role;
import com.qxl.forest.common.util.ToolUtil;
import com.qxl.forest.core.cache.CacheKit;
import com.qxl.forest.exception.BizExceptionEnum;
import com.qxl.forest.exception.BussinessException;
import com.qxl.forest.util.CommRes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: qiuxinlin
 * @Dercription: 角色控制器
 * @Date: 2017/12/7
 */
@RestController
@RequestMapping("/role")
public class RoleController extends BaseController {

    @Autowired
    RoleDao roleDao;
    @Autowired
    RoleService roleService;

    /**
     * 获取角色列表
     *
     * @param requestParams
     * @return
     */
    @Permission
    @PostMapping(value = "/get")
    public CommRes getAll(@RequestBody RequestParams requestParams) {
        if (requestParams.getPageSize() == 0) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        PageRequest pageRequest = new PageRequest(requestParams.getPageNumber(), requestParams.getPageSize());
        Page roles = roleDao.findAll(pageRequest);
        ConstantFactory constantFactory = (ConstantFactory) ConstantFactory.me();
        for (Role role : (List<Role>) roles.getContent()) {
            String pname = constantFactory.getRolePnameByPid(role.getPid());
            role.setPname(pname);
            String deptName = constantFactory.getDeptNameByRoleId(role.getId());
            role.setDeptName(deptName);
        }
        return CommRes.success(roles);
    }

    /**
     * 角色新增
     *
     * @param role
     * @return
     */
    @PostMapping(value = "/add")
    @BussinessLog(value = "添加角色", key = "role")
    @Permission(Const.ADMIN_NAME)
    public CommRes add(@RequestBody Role role) {
        roleDao.save(role);
        return CommRes.successRes();
    }

    /**
     * 角色修改
     *
     * @param role
     * @return
     */
    @PutMapping(value = "/update")
    @BussinessLog(value = "修改角色", key = "role")
    @Permission(Const.ADMIN_NAME)
    public CommRes edit(@RequestBody Role role) {
        roleDao.save(role);
        //删除缓存
        //CacheKit.removeAll(Cache.CONSTANT);
        return CommRes.successRes();
    }

    /**
     * 删除角色
     *
     * @param roleId
     * @return
     */
    @DeleteMapping(value = "/delete/{roleId}")
    @BussinessLog(value = "删除角色", key = "roleId")
    @Permission(Const.ADMIN_NAME)
    public CommRes remove(@PathVariable Integer roleId) {
        //不能删除超级管理员角色
        if (roleId.equals(Const.ADMIN_ROLE_ID)) {
            throw new BussinessException(BizExceptionEnum.CANT_DELETE_ADMIN);
        }
        //缓存被删除的角色名称
        //LogObjectHolder.me().set(ConstantFactory.me().getSingleRoleName(roleId));

        roleService.delRoleById(roleId);
        //删除缓存
        CacheKit.removeAll(Cache.CONSTANT);
        return CommRes.successRes();
    }

    /**
     * 配置权限
     *
     * @param requestParams 里面必须包含r_roleId和r_menuIds
     * @return
     */
    @PostMapping("/setAuthority")
    @BussinessLog(value = "配置权限", key = "requestParams")
    @Permission(Const.ADMIN_NAME)
    public CommRes setAuthority(@RequestBody RequestParams requestParams) {
        if (ToolUtil.isOneEmpty(requestParams.getR_roleId())) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        roleService.setAuthority(requestParams.getR_roleId(), requestParams.getR_menuIds());
        return CommRes.successRes();
    }

    /**
     * 获取角色树形列表 通过elementUi的tree组件的懒加载实现
     *
     * @param pid
     * @return
     */
    @GetMapping("/selectRoleTreeList/{pid}")
    @Permission
    public CommRes selectRoleTreeList(@PathVariable Integer pid) {
        List roles = roleDao.findByPid(pid);
        return CommRes.success(roles);
    }
}
